﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;

namespace QuanLyTruongHoc_App
{
    public partial class QLMonHoc : Form
    {
        public QLMonHoc()
        {
            InitializeComponent();
            dg_DanhSach.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dg_DanhSach_DataBindingComplete);
        }

        private void QuanLyGiaoVien_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void refreshData()
        {
            view_MonHocTableAdapter.Connection.ConnectionString =
                 PublicLib.getStrConn();

            this.view_MonHocTableAdapter.FillByFlag(this.qLTH.View_MonHoc, PConst.FLAG_NONE);
            reloadData();
        }

        private void QuanLyGiaoVien_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 20;
            dg_DanhSach.Height = this.Height - pn_TimKiem.Height - 50;

            pn_TimKiem.Top = pn_Button.Top = dg_DanhSach.Bottom + 5;
            pn_Button.Left = dg_DanhSach.Right - pn_Button.Width;
        }

        private void dg_DanhSach_DataBindingComplete(object sender, EventArgs e)
        {
            reloadData();
        }

        private void reloadData()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                int tmp = int.Parse(dg_DanhSach.Rows[i].Cells[tinhLenLopDataGridViewTextBoxColumn.Name].Value.ToString());
                dg_DanhSach.Rows[i].Cells[cl_TinhHK.Name].Value = ((tmp & AddEditMonHoc.FLAG_DIEM_HK) != 0);
                dg_DanhSach.Rows[i].Cells[cl_LenLop.Name].Value = ((tmp & AddEditMonHoc.FLAG_TINH_LEN_LOP) != 0);
            }
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            AddEditMonHoc frm_ThemMH = new AddEditMonHoc();
            frm_ThemMH.Show();
            frm_ThemMH.Disposed += new EventHandler(frm_ThemMH_Disposed);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            AddEditMonHoc frm_EditMH = new AddEditMonHoc();
            frm_EditMH.Show();
            frm_EditMH.setEditInfo(int.Parse(dg_DanhSach.SelectedRows[0].Cells[idDataGridViewTextBoxColumn.Name].Value.ToString()));
            frm_EditMH.Disposed += new EventHandler(frm_ThemMH_Disposed);
        }

        private void frm_ThemMH_Disposed(object sender, EventArgs e)
        {
            refreshData();
        }

        private void dg_DanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            bt_Sua_Click(bt_Sua, new EventArgs());
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;

            deleteGV();
            refreshData();
        }

        private void deleteGV()
        {
            String idMH = dg_DanhSach.SelectedRows[0].Cells[idDataGridViewTextBoxColumn.Name].Value.ToString();
            MyDataSet.QLTHTableAdapters.tbl_MonHocTableAdapter adap = new tbl_MonHocTableAdapter();
            adap.Connection.ConnectionString = PublicLib.getStrConn();
            adap.Fill(qLTH.tbl_MonHoc);

            DataRow gv = qLTH.tbl_MonHoc.Select(qLTH.tbl_MonHoc.idColumn.ColumnName + "=" + idMH)[0];
            gv[qLTH.tbl_MonHoc.flagColumn] = PConst.FLAG_DELETE;

            adap.Update(gv);
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            deleteGV();
        }
    }
}
